<?php
/*
 * @Author: KingMin 
 * @Date: 2021-07-13 22:34:59 
 * @Last Modified by:   KingMin 
 * @Last Modified time: 2021-07-13 22:34:59 
 * @The authors website: https://www.kingmin.cn  
 */
/**
 * 配置查询及设置
 * @copyright (c) KINGMIN All Rights Reserved
 */

class Group_Model {
    // 缓存组合数据列表
    public static function getGroupCacheList(){
        $DB=Database::getInstance();
        
	    $sql = "SELECT * FROM `" . DB_PREFIX . "sys_group` WHERE `isdel`='0';";
        $logs = $DB->query($sql);
        // $output['sql']=$sql;
        $data = array();
        foreach ($logs as $key => $v) {
            $gid=$v['id'];
            $sql = "SELECT * FROM `" . DB_PREFIX . "sys_group_data` WHERE `isdel`='0' AND `gid`='$gid' ORDER BY `order_num` ASC ;";
            $logs = $DB->query($sql);
            // $output['sql']=$sql;
            $dt = array();
            foreach ($logs as $k => $d) {
                
                $d=json_decode($d['value'],true);
                $d['fields']=json_decode($v['fields'],true);
                $dt[]=$d;
            }
            $data[$v['config_name']]=$dt;
        }
        // $output['list']=$data;
        return $data;
    }
    // 组合数据列表
    public static function getGroupList($pages=[],$dt=[]){
        $DB=Database::getInstance();
        @$isAll=$pages['isAll'];
        $output = [
			'pages'=>[
			    'isAll'=>$isAll?true:false,
    			'recordCount' => 0,     //满足条件的总记录数
    		    'pageSize' => 10,        //每页大小，即每页最多可以显示的记录数量
    		    'pageCount' => 0,       //总页数
    		    'pno' => 1,          //当前数据所在页号
			 ],
			'list' => [] ,         //当前页中的数据
	    ];
	    $bySql="ORDER BY `id` DESC ";
	    $isfym="";$kSql="";$rSql="";$dSql="";
	    if(@$pages['pno']){
		  $output['pages']['pno'] = intval($pages['pno']);
		}
		if(@$pages['pageSize']){
		  $output['pages']['pageSize']= intval($pages['pageSize']);
		}
	    $pno = $output['pages']['pno'];$pgs =$output['pages']['pageSize'];
	    if(@$dt['key']){
	       $kSql=" AND ( `id`='".$dt['key']."' OR `name` LIKE '%".$dt['key']."%' OR `config_name` LIKE '%".$dt['key']."%' ) ";
	    }
	    $totalSql = " SELECT COUNT(*) as total  FROM `" . DB_PREFIX . "sys_group` WHERE `isdel`='0' $kSql $rSql $dSql ;";
		$res = $DB->fetch_array($DB->query($totalSql));
		$output['pages']['recordCount'] = intval($res['total']);
		//计算总页数
		$output['pages']['pageCount'] = ceil($output['pages']['recordCount']/$output['pages']['pageSize']);
		//获取指定页中的数据
		$start = ($output['pages']['pno']-1)*$output['pages']['pageSize'];
		$count = $output['pages']['pageSize'];
		if(!$output['pages']['isAll']){
			$isfym="LIMIT $start,$count";
	    }
	    
	    $sql = "SELECT * FROM `" . DB_PREFIX . "sys_group` WHERE `isdel`='0' $kSql $rSql $dSql $bySql  $isfym;";
        $logs = $DB->query($sql);
        // $output['sql']=$sql;
        $data = array();
        foreach ($logs as $key => $v) {
            
            $v['fields']=json_decode($v['fields'],true);
            
            $data[]=$v;
        }
        $output['list']=$data;
        return $output;
    }
    // 组合数据详情列表
    public static function getGroupDataList($pages=[],$dt=[]){
        $DB=Database::getInstance();
        @$isAll=$pages['isAll'];
        $output = [
			'pages'=>[
			    'isAll'=>$isAll?true:false,
    			'recordCount' => 0,     //满足条件的总记录数
    		    'pageSize' => 10,        //每页大小，即每页最多可以显示的记录数量
    		    'pageCount' => 0,       //总页数
    		    'pno' => 1,          //当前数据所在页号
			 ],
			'list' => [] ,         //当前页中的数据
	    ];
	    $bySql="ORDER BY `order_num` ASC ";
	    $isfym="";$kSql="";$rSql="";$dSql="";
	    if(@$pages['pno']){
		  $output['pages']['pno'] = intval($pages['pno']);
		}
		if(@$pages['pageSize']){
		  $output['pages']['pageSize']= intval($pages['pageSize']);
		}
	    $pno = $output['pages']['pno'];$pgs =$output['pages']['pageSize'];
	    if(@$dt['status']){
	       $kSql=" AND `status`='".$dt['status']."' ";
	    }
	    if(@$dt['gid']){
	       $rSql=" AND `gid`='".$dt['gid']."' ";
	    }else {
	        return $output;
	    }
	    $totalSql = " SELECT COUNT(*) as total  FROM `" . DB_PREFIX . "sys_group_data` WHERE `isdel`='0' $kSql $rSql $dSql ;";
		$res = $DB->fetch_array($DB->query($totalSql));
		$output['pages']['recordCount'] = intval($res['total']);
		//计算总页数
		$output['pages']['pageCount'] = ceil($output['pages']['recordCount']/$output['pages']['pageSize']);
		//获取指定页中的数据
		$start = ($output['pages']['pno']-1)*$output['pages']['pageSize'];
		$count = $output['pages']['pageSize'];
		if(!$output['pages']['isAll']){
			$isfym="LIMIT $start,$count";
	    }
	    
	    $sql = "SELECT * FROM `" . DB_PREFIX . "sys_group_data` WHERE `isdel`='0' $kSql $rSql $dSql $bySql  $isfym;";
        $logs = $DB->query($sql);
        // $output['sql']=$sql;
        $data = array();
        foreach ($logs as $key => $v) {
            
            $v['value']=json_decode($v['value'],true);
            $v['order_num']=intval($v['order_num']);
            $v['statusT']=$v['status']=='1'?true:false;
            $data[]=$v;
        }
        $output['list']=$data;
        $output['tabData']=self::getGroupDetailId($dt['gid']);
        return $output;
    }
    public static function getGroupDetailKey($key){
        if(!$key){
            return false;
        }
        $DB=Database::getInstance();
        
        $res = $DB->query("SELECT * FROM `".DB_PREFIX."sys_group` where `config_name`='$key' ");
        $config =false;
        while($row = $DB->fetch_array($res)) {
            $row['fields']=json_decode($row['fields'],true);
            $config=$row;
        }
        return $config;
    }
    public static function getGroupDetailId($id){
        if(!$id){
            return false;
        }
        $DB=Database::getInstance();
        
        $res = $DB->query("SELECT * FROM `".DB_PREFIX."sys_group` where `id`='$id' ");
        $config =false;
        while($row = $DB->fetch_array($res)) {
            $row['fields']=json_decode($row['fields'],true);
            foreach ($row['fields'] as $i => $v){
                if(@$v['parameter']){
                    $parameter=explode("\n",$v['parameter']);
                    $rd=[];
                    foreach($parameter as $k => $d ){
                        $n=explode("=>",$d);
                        $nl=[];
                        $rd[$n[0]]=isBoolStr($n[1]);
                        // $rd=$nl;
                        // array_push($rd,$nl);
                    }
                    $row['fields'][$i]['parameterList']=$rd;
                }
            }
            $config=$row;
        }
        return $config;
    }
    public static function getGroupDataDetail($id){
        if(!$id){
            return false;
        }
        $DB=Database::getInstance();
        
        $res = $DB->query("SELECT * FROM `".DB_PREFIX."sys_group_data` where `id`='$id' ");
        $config =false;
        while($row = $DB->fetch_array($res)) {
           $row['value']=json_decode($row['value'],true);
           $config=$row;
        }
        return $config;
    }
    // 新增数据
     // 新增
    public static function addGroup($dt=[]){
        $DB=Database::getInstance();
        if(!is_array($dt)&&count($dt<=0)){
            return false;
        }
        $fields=self::getGroupDetailKey(@$dt['config_name'])??true;
        if($fields){
            return false;
        }
        
        $sql=addSqlConvert('sys_group',$dt);
        // return $sql;
        $rt=true;
        $rt=$DB->query($sql);
        $id=$DB->insert_id();
        $CACHE = Cache::getInstance();
        $CACHE->updateCache('groups');
        return $rt;
    }
     // 新增
    public static function addGroupData($dt=[]){
        $DB=Database::getInstance();
        if(!is_array($dt)&&count($dt<=0)){
            return false;
        }
        
        $sql=addSqlConvert('sys_group_data',$dt);
        // return $sql;
        $rt=true;
        $rt=$DB->query($sql);
        $id=$DB->insert_id();
        $CACHE = Cache::getInstance();
        $CACHE->updateCache('groups');
        return $rt;
    }
    
    // 修改
    public static function uploadGroup($dt,$id){
        $DB=Database::getInstance();
        if(!$id){
            return false;
        }
        if(!is_array($dt)&&count($dt<=0)){
            return false;
        }
        
        $rt=true;
        if(count($dt)>0){
            $sql=upSqlConvert('sys_group',$dt,"`id`='$id'");
            // return $sql;
            $rt=$DB->query($sql);
            $CACHE = Cache::getInstance();
            $CACHE->updateCache('groups');
        }
        return $rt;
    }
    // 修改
    public static function uploadGroupData($dt,$id){
        $DB=Database::getInstance();
        if(!$id){
            return false;
        }
        if(!is_array($dt)&&count($dt<=0)){
            return false;
        }
        
        $rt=true;
        if(count($dt)>0){
            $sql=upSqlConvert('sys_group_data',$dt,"`id`='$id'");
            // return $sql;
            $rt=$DB->query($sql);
            $CACHE = Cache::getInstance();
            $CACHE->updateCache('groups');
        }
        return $rt;
    }
    
}